// 部门的增删改查
const express=require("express");
const moment=require("moment")
// 引入数据库操作封装
const db=require("../utils/db.js");
const router=express.Router();

//查询
router.get("/search",(req,res)=>{
    let {departmentName}=req.query;
    let sql=`select * from department where departmentName like '%${departmentName}%'`;
    db.execute(sql, (rst) => {
        res.send({
            code: 0,
            data: rst
        })
    })
})

// 查询所有部门信息
router.get("/queryAll",(req,res)=>{
    let sql=`select id,departmentName from department`;
    db.execute(sql, (rst) => {
        res.send({
            code: 0,
            data: rst
        })
    })
})


// 根据id删除一条数据
router.get("/delete", (req, res) => {
    let {
        id
    } = req.query;
    let sql = `delete from department where id=${id}`;
    db.execute(sql, (rst) => {
        if (rst.affectedRows != 0) {
            res.send({
                code: 0,
                msg: "删除成功"
            })
        } else {
            res.send({
                code: 1,
                msg: "删除失败"
            })
        }
    })
})
// 添加
router.post("/save", (req, res) => {
    let {
        departmentName,
        remarks
    } = req.body;
    let crateTime=moment(new Date()).format('YYYY-MM-DD hh:mm:ss');
    let sql = `insert into department (departmentName,remarks,departmentCount,crateTime) values ('${departmentName}','${remarks}',0,'${crateTime}')`;
    db.execute(sql, (rst) => {
        if (rst.affectedRows != 0) {
            res.send({
                code: 0,
                msg: "添加成功"
            })
        } else {
            res.send({
                code: 1,
                msg: "添加失败"
            })
        }
    })
})

//根据id查询一条数据
router.get("/findOne",(req,res)=>{
    //接收前台传递的id
   let {id}= req.query;
   let sql=`select * from department where id=${id}`;
   db.execute(sql,(rst)=>{
    //将一个对象响应给前台
    if(rst.length==0){
        res.send({
            code:1,
            msg:"无当前数据"
        })
    }else{
        res.send({
            code:0,
            msg:"查询成功",
            data:rst[0]
        })
    }
   })
})


//根据id修改一条数据
router.post("/update",(req,res)=>{
    let {
        departmentName,
        remarks,
        id
    } = req.body;
    let sql=`update department set departmentName='${departmentName}',remarks='${remarks}' where id=${id}`;
    db.execute(sql,(rst)=>{
        if(rst.affectedRows!=0){
            res.send({
                code:0,
                msg:"修改成功"
            })
        }else{
            res.send({
                code:1,
                msg:"修改失败"
            })
        }
    })
})


// 接收部门id,修改数量
router.post("/setcount",(req,res)=>{
    let {id,fh}=req.body;
    let sql="";
    if(fh=="+"){
        sql=`update department set departmentCount=departmentCount+1 where id=${id}`
    }else{
        sql=`update department set departmentCount=departmentCount-1 where id=${id}`
    }
    db.execute(sql,(rst)=>{
        if(rst.affectedRows!=0){
            res.send({
                code:0,
                msg:"数量修改成功"
            })
        }else{
            res.send({
                code:1,
                msg:"数量修改失败"
            })
        }
    })
})


module.exports=router;